home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 844 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: lrz-muenchen.de!sun2!ua302aa
  2. From: ua302aa@sun2.lrz-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is '?' in C mean....?????
  5. Date: 9 Jan 1996 08:21:04 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4ct8hg$mtl@sparcserver.lrz-muenchen.de>
  9. References: <4cgsa8$bm2@wumpus.cc.uow.edu.au> <fcusack-0401961115540001@mudskipper.cac.psu.edu> <4ci5bb$8m4@www.gnofn.org>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. clm01@www.gnofn.org (Christopher L Mayeux) writes:
  13.  
  14. > frank. (fcusack@tdx.org) wrote:
  15. >: > Could anyone here explain to me what is "?" means and what the purpose
  16. >: of using
  17. >:
  18. >: ? : is C's ternary operator. if the condition is met, perform the
  19. >: operation after the ?; if the condition is not met, perform the operation
  20. >: after the :
  21. >:
  22.  
  23. >What new perversion of C is that ???
  24.  
  25. >I've been programming in standard C for 12 years, and
  26. >never saw THAT in the manuals.
  27.  
  28. If both the "operation" before the colon and the "operation" behind the
  29. colon are expressions that are evaluated only for their side effects,
  30. the wording is still poor but you _can_ do something like
  31.  
  32.   x > y ? 
  33.     printf("x is greater than y") : 
  34.     printf("x is not greater than y");
  35.  
  36. An expressin that uses C's only ternary operator is called a conditional
  37. expression, and conditional expressions make a lot of sense, esp. if you
  38. try to write C functions in the style of a functional programming language.
  39.  
  40.   unsigned int fak(unsigned int n) { return x < 2 ? 1 : n * fak(n - 1); }
  41.  
  42. or if the conditional expression is part of a large expression.
  43.  
  44. Conditional expressions where part of the C programming language
  45. at least since K&R 1. I would not call them perversions at all,
  46. but you can be sure that they are no _new_ perversions.
  47.  
  48. Kurt
  49. --
  50. | Kurt Watzka                             Phone : +49-89-2180-6254
  51. | watzka@stat.uni-muenchen.de
  52. | ua302aa@sunmail.lrz-muenchen.de
  53.